package com.gdth.bank.presenter;


import java.util.List;

import com.gdth.bank.model.TXtBmModel;
import com.gdth.bank.model.WebViewModel;
import com.gdth.bank.view.WebViewView;
import com.gdth.database.bean.TXtBm;
import com.gdth.utils.GsonUtil;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class WebViewPresenter {
	private WebViewView mWebViewView;
	private WebViewModel mWebViewModel;
	private TXtBmModel mTXtBmModel;
	
	public WebViewPresenter(WebViewView mWebViewView){
		this.mWebViewView = mWebViewView;
		this.mWebViewModel = new WebViewModel();
		this.mTXtBmModel = new TXtBmModel();
	}
	
	public void getAllBm(){
		mTXtBmModel.getAllBm("")
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<TXtBm>>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
				mWebViewView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				//System.out.println(arg0);
			}

			@Override
			public void onNext(List<TXtBm> arg0) {
				// TODO Auto-generated method stub
				
				String val = GsonUtil.getGson().toJson(arg0);
				
				mWebViewView.returnBm(val);
			}
		});
	}
	
	public void getData(String data, final int type){
		mWebViewModel.getData(data, type)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<String>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
//				mWebViewView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mWebViewView.showMessage(arg0.getMessage());
			}

			@Override
			public void onNext(String arg0) {
				// TODO Auto-generated method stub
				
				//System.out.println(arg0);
				mWebViewView.returnData(arg0, type);
			}
		});
	}
	
	//url请求新方法
	public void getData(String data, String url,final String callback){
		mWebViewModel.getData(url,data)
		.subscribeOn(Schedulers.io())
		.unsubscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<String>(){

			@Override
			public void onCompleted() {
				// TODO Auto-generated method stub
//				mWebViewView.hideProgressDialog();
			}

			@Override
			public void onError(Throwable arg0) {
				// TODO Auto-generated method stub
				mWebViewView.showMessage(arg0.getMessage());
			}

			@Override
			public void onNext(String arg0) {
				// TODO Auto-generated method stub
				//System.out.println(arg0);
				mWebViewView.returnData(arg0, callback);
			}
		});
	}
}
